package com.niodata.dp.monitor.common.util;

import com.google.common.base.Strings;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

public class ConfigUtil {

  private static ConfigUtil instance = new ConfigUtil();
  private static Properties properties;

  static {
    PathMatchingResourcePatternResolver resourceLoader = new PathMatchingResourcePatternResolver();
    try {
      properties = new Properties();
      Resource[] resources = resourceLoader.getResources("classpath:*.properties");
      for (Resource resource : resources) {
        Properties props = new Properties();
        InputStream input = resource.getInputStream();
        props.load(input);
        input.close();
        properties.putAll(props);
      }
    } catch (IOException e) {
      //
    }
  }

  private ConfigUtil() {
  }

  public static ConfigUtil reload() {
    instance = new ConfigUtil();
    return instance;
  }

  public static ConfigUtil getInstance() {
    return instance;
  }

  public String getString(String str) {
    return properties.getProperty(str);
  }

  public String getString(String str, String defaultValue) {
    return Strings.isNullOrEmpty(properties.getProperty(str)) ? defaultValue
          : properties.getProperty(str);
  }

}
